package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.lkt.common.util.Query;
import io.lkt.common.utils.DateUtil;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.service.BPlatformBeginnerArticleTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.BBeginnerArticleTypeEntity;
import io.lkt.modules.sys.service.BBeginnerArticleTypeService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 新手学堂文章类别表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2021-12-15 15:16:02
 */
@RestController
@RequestMapping("/sys/bbeginnerarticletype")
@Slf4j
public class BBeginnerArticleTypeController {
    @Autowired
    private BBeginnerArticleTypeService bBeginnerArticleTypeService;

    @Autowired
    private BPlatformBeginnerArticleTypeService bPlatformBeginnerArticleTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:bbeginnerarticletype:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bBeginnerArticleTypeService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/queryList")
//    @RequiresPermissions("sys:bbeginnerarticletype:list")
    public R queryList(@RequestParam Map<String, Object> params){
        Query query = new Query(params);

        List<BBeginnerArticleTypeEntity> list = bBeginnerArticleTypeService.queryList(query);
        int total = bBeginnerArticleTypeService.queryListTotal(query);

        PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:bbeginnerarticletype:info")
    public R info(@PathVariable("id") Integer id){
        BBeginnerArticleTypeEntity bBeginnerArticleType = bBeginnerArticleTypeService.getById(id);
        List<String> gzhCodeList = bPlatformBeginnerArticleTypeService.getGzhCodeList(id);
        bBeginnerArticleType.setGzhCodeList(gzhCodeList);

        return R.ok().put("bBeginnerArticleType", bBeginnerArticleType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:bbeginnerarticletype:save")
    public R save(@RequestBody BBeginnerArticleTypeEntity bBeginnerArticleType){
        bBeginnerArticleTypeService.saveObject(bBeginnerArticleType);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:bbeginnerarticletype:update")
    public R update(@RequestBody BBeginnerArticleTypeEntity bBeginnerArticleType){
        log.info("[update ] = {}", bBeginnerArticleType);
        ValidatorUtils.validateEntity(bBeginnerArticleType);
        bBeginnerArticleType.setModifyTime(DateUtil.getCurDate());
        bBeginnerArticleTypeService.updateById(bBeginnerArticleType);

        // 更新平台文章类型(新手学堂)
        List<String> gzhCodeList = bBeginnerArticleType.getGzhCodeList();
        if (CollectionUtils.isNotEmpty(gzhCodeList)) {
            bPlatformBeginnerArticleTypeService.updateObject(bBeginnerArticleType.getId(), gzhCodeList);
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:bbeginnerarticletype:delete")
    public R delete(@RequestBody Integer[] ids){
        bBeginnerArticleTypeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
